package GUILayer;
import ControlLayer.CtrShowOrder;
import ModelLayer.Order;
import ModelLayer.Customer;
import ModelLayer.Address;
import ModelLayer.Country;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.Date;

public class UIOrderList extends javax.swing.JFrame {
    private CtrShowOrder ctrShowOrder;

    public UIOrderList() {
        initComponents();
        ctrShowOrder = CtrShowOrder.getInstance();
        loadTableContent();
    }

    public Date convertUnixtime(int time) {
        Date date = new Date((long)time * 1000);
        return date;
    }

    private void loadTableContent() {
        ((DefaultTableModel)tblOrderList.getModel()).setRowCount(0);
        ArrayList<Order> orderList = ctrShowOrder.getOrderList();
        for (Order order : orderList) {
            Customer customer   = ctrShowOrder.getCustomerByID(order.getIDCustomer());
            Address address     = ctrShowOrder.getAddressByID(order.getIDAddressInvoice());
            Country country     = ctrShowOrder.getCountryByID(address.getIDCountry());
            Date dateAdd        = convertUnixtime(order.getDateAdd());

            ((DefaultTableModel)tblOrderList.getModel()).addRow(
                new Object [] {
                    order.getIDOrder(),
                    dateAdd.toString(),
                    order.getInvoiceNumber(),
                    customer.getFirstname() + " " + customer.getLastname(),
                    address.getAddress1() + ", " + address.getPostcode() + " " + address.getCity() + ", " + country.getIsoCode(),
                    order.getTotalPaidReal(),
                    order.getPayment(),
                    order.getInvoiceDate()
                }
            );
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrlOrderList = new javax.swing.JScrollPane();
        tblOrderList = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblOrderList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Added", "Invoice Number", "Customer", "Address", "Amount", "Payment", "Invoice Date"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Float.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblOrderList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblOrderListMouseClicked(evt);
            }
        });
        scrlOrderList.setViewportView(tblOrderList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlOrderList, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlOrderList, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblOrderListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblOrderListMouseClicked
        Integer orderID = Integer.parseInt(tblOrderList.getValueAt(tblOrderList.getSelectedRow(), 0).toString());
        UIShowOrder uiShowOrder = new UIShowOrder();
        uiShowOrder.setVisible(true);
        uiShowOrder.loadOrderForm(orderID);
    }//GEN-LAST:event_tblOrderListMouseClicked

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UIOrderList().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scrlOrderList;
    private javax.swing.JTable tblOrderList;
    // End of variables declaration//GEN-END:variables

}
